﻿using SG;
using System;
using System.Collections.Generic;
using UnityEngine;





public class PanelBase : MonoBehaviour
{
    protected GameObject m_BGNode;
    [HideInInspector]
    public string m_strBGProfab = ""; // 底板profab
    [HideInInspector]
    public int m_nTileGroupID = -1;      // 标题栏类型
    [HideInInspector]
    public string m_strTitleImage;      // 标题字符图片

    string m_strPanelName;
    protected string m_strLastWindow;
    public string PanelName
    {
        get { return m_strPanelName; }
        set { m_strPanelName = value; }
    }

    int m_nCamera;
    public int CameraIndex
    {
        get { return m_nCamera; }
    }
    public void SetCamera(int nCamera = 0)
    {
        m_nCamera = nCamera;
        switch (m_nCamera)
        {
            case 2: // 3d摄像机
                Canvas.worldCamera = UIMgr.Instance().m_uiCamera3d;
                gameObject.SetAllChildLayer("UI3D");
                break;
            case 1: //  背景摄像机
                UIMgr.Instance().m_uiCamera2dBack.enabled = true;
                Canvas.worldCamera = UIMgr.Instance().m_uiCamera2dBack;
                gameObject.SetAllChildLayer("UI");
                break;
            default:// 正常摄像机
                UIMgr.Instance().m_uiCamera2d.enabled = true;
                Canvas.worldCamera = UIMgr.Instance().m_uiCamera2d;
                gameObject.SetAllChildLayer("UI");
                break;
        }
        //UIMgr.Instance().DoEnableCamera(nCamera, true);
    }
    protected Canvas m_Canvas = null;

    public Canvas Canvas
    {
        get { return m_Canvas; }
        set { m_Canvas = value; }
    }

    private int m_Layer = 0;
    private int m_DefaultSortOrder = 0;
    private int m_SortOrder = 0;

    #region 界面所在组
    private int m_nGroup;
    public int UIGroup
    {
        get { return m_nGroup; }
        set { m_nGroup = value; }
    }
    #endregion

    public bool IsNeedNavigate()
    {
        return Navigate > 0;
    }
    [HideInInspector]
    public int Navigate = 0; // 0 是不关注
    [HideInInspector]
    public int NavigateType = 0;
    [HideInInspector]
    public int FullWindow = 0; // 1是全界面，遮挡住下面的界面
    public bool IsFullWindow()
    {
        return FullWindow > 0;
    }
    [HideInInspector]
    public int ShowBg = 0;
    [HideInInspector]
    public int UseSceneCamera = 0;
    public bool IsShowBg()
    {
        return ShowBg > 0;
    }
    [HideInInspector]
    public int QuickNavSkipClose = 0; // 
    public bool IsQuickNavSkipClose()
    {
        return QuickNavSkipClose > 0;
    }

    public int Layer
    {
        get { return m_Layer; }
        set
        {
            m_Layer = value;
            if (m_Canvas != null)
            {
                m_Canvas.planeDistance = m_Layer * 200;
            }
        }
    }

    //是否被遮住
    [HideInInspector]
    public bool IsCoverUp { set; get; }

    Transform m_RootTransform = null;
    protected Transform GetRootTransform()
    {
        if (m_RootTransform == null)
            return gameObject.transform;
        return m_RootTransform;
    }



    //void CreateAnimProfabe(string strName, List<UITweener> list)
    //{
    //    list.Clear();
    //    GameObject obj = null;
    //    Dictionary<string, GameObject> aniPrefabList = UIMgr.Instance().AniPrefabList;
    //    if (aniPrefabList.ContainsKey(strName))
    //    {
    //        obj = aniPrefabList[strName];
    //    }
    //    else
    //    {
    //        var prefab = (GameObject)CoreEntry.gResLoader.Load(strName, typeof(GameObject));
    //        if (prefab == null)
    //        {
    //            XKLog.Log("没有这个动画模板文件:[" + strName + "]");
    //            return;
    //        }
    //        obj = Instantiate(prefab) as GameObject;
    //        obj.transform.SetParent(aniPrefabList["Root"].transform);
    //        aniPrefabList.Add(strName, obj);
    //    }
    //    //obj.transform.SetParent(GetRootTransform());//设置成子物体ui销毁动画模板也会销毁
    //    var comList = obj.GetComponents<UITweener>();
    //    for( int i = 0; i < comList.Length; ++ i )
    //    {
    //        if (comList[i] is TweenAlphaEx)
    //        {
    //            var item = GetRootTransform().gameObject.AddComponent<TweenAlphaEx>();
    //            var src = comList[i] as TweenAlphaEx;
    //            CopyTween(src, item);
    //            item.from = src.from;
    //            item.to = src.to;
    //            list.Add(item);
    //        }
    //        else if (comList[i] is TweenScale)
    //        {
    //            var item = GetRootTransform().gameObject.AddComponent<TweenScale>();
    //            var src = comList[i] as TweenScale;
    //            CopyTween(src, item);
    //            item.from = src.from;
    //            item.to = src.to;
    //            list.Add(item);
    //        }
    //        else if (comList[i] is TweenRotation)
    //        {
    //            var item = GetRootTransform().gameObject.AddComponent<TweenRotation>();
    //            var src = comList[i] as TweenRotation;
    //            CopyTween(src, item);
    //            item.from = src.from;
    //            item.to = src.to;
    //            item.quaternionLerp = src.quaternionLerp;
    //            list.Add(item);
    //        }
    //        else if (comList[i] is TweenPosition )
    //        {
    //            var item = GetRootTransform().gameObject.AddComponent<TweenPosition>();
    //            var src = comList[i] as TweenPosition;
    //            CopyTween(src, item);
    //            item.from = src.from;
    //            item.to = src.to;
    //            item.worldSpace = src.worldSpace;
    //            list.Add(item);
    //        }
    //    }
    //}

    //void CopyTween(  UITweener src, UITweener item  )
    //{
    //    item.animationCurve = CopyAnimationCuve(src.animationCurve);
    //    item.delay = src.delay;
    //    item.duration = src.duration;
    //    item.ignoreTimeScale = src.ignoreTimeScale;
    //    item.useFixedUpdate = src.useFixedUpdate;
    //    item.tweenGroup = src.tweenGroup;
    //    item.enabled = false;
    //}
    //public void InitDisplayAnim(string strShow, string strHide)
    //{
    //    m_RootTransform = transform.Find("BackNode");
    //    m_AniShow.Clear();
    //    m_AniHide.Clear();
    //    if (!string.IsNullOrEmpty(strShow))
    //        CreateAnimProfabe(strShow, m_AniShow);
    //    if (!string.IsNullOrEmpty(strHide))
    //        CreateAnimProfabe(strHide, m_AniHide);

    //}
    void Init()
    {

        if (m_strBGProfab == "")
            return;
        if (m_BGNode == null)
        {
            ObjectMgr.Instance().CreateObjAysnc(m_strBGProfab, null, (_node) =>
            {
                InitBgNode(_node);
            });
        }
    }

    void InitBgNode(GameObject _node)
    {
        m_BGNode = _node;
        if (m_BGNode == null)
        {
            XKLog.Log("没有这个文件:[" + m_strBGProfab + "]，无法创建窗口底板，确认是否故意如此!!");
            return;
        }
        m_BGNode.transform.SetParent(GetRootTransform());
        m_BGNode.transform.SetSiblingIndex(0);
        m_BGNode.SetActive(true);
        var rc = m_BGNode.GetComponent<RectTransform>();
        rc.localScale = Vector3.one;
        rc.localPosition = Vector3.zero;
    }

    public int DefaultSortOrder
    {
        get { return m_DefaultSortOrder; }
        set { m_DefaultSortOrder = value; }
    }
    public int SortOrder
    {
        get { return m_SortOrder; }
        set
        {
            m_SortOrder = value;
            if (m_Canvas != null)
            {
                m_Canvas.overrideSorting = true;
                m_Canvas.sortingOrder = value;
            }
        }
    }
    public void SetCanvasEnabled(bool isEnable)
    {
        if (m_Canvas != null)
        {
            m_Canvas.enabled = isEnable;
        }
    }
    protected Dictionary<string, GameObject> subObjects = new Dictionary<string, GameObject>();


    //上次打开的时间
    protected float m_lastShowTime = 0;
    public float LastShowTime
    {
        get { return m_lastShowTime; }
        set { m_lastShowTime = value; }
    }



    // 是否显示
    protected bool isShown;
    public bool IsShown { get { return isShown; } }



    // 自动释放时,  增加 延迟 m_inMemoryTime  秒后才释放
    protected float m_inMemoryTime = 0;

    public float InMemoryTime
    {
        get { return m_inMemoryTime; }
        set { m_inMemoryTime = value; }
    }




    protected Action AniCallFun;//全屏对话框动画结束回调




    // 关闭界面，只能lua调用
    public void DoClose()
    {
        UIMgr.Instance().DoCloseWindow(PanelName);
        if (m_bUseTemp)
            PopTemp();
    }

    public bool DoHide(bool bCache)
    {
        if (IsCache || bCache)
        {
            gameObject.SetActive(false);
            return true;
        }
        else
        {
            gameObject.StopOrDestroy();
        }
        var cmd = new EventParamCommon();
        cmd.AddDataString("window", PanelName);
        EventMgr.Instance().TriggerEventDelay((int)SystemEventDef.System_Close_Window, cmd);
        return false;
    }
    // lua界面base层不允许调用doshow
    protected virtual void OnParam(EventParam param = null)
    {

    }
    public void DoShow(EventParam param = null)
    {
        gameObject.SetActive(true);
        OnParam(param);
    }

    // lua不要调用这个,

    // 把这个界面扔到视线外来形成关闭效果
    public void DoFaraway()
    {
        if (Canvas != null)
            Canvas.planeDistance = 1000000;
    }
    // 给这个界面设定摄像机
    public void DoSetCamera(Camera car = null)
    {
        if (car == null)
            Canvas.worldCamera = UIMgr.Instance().m_uiCamera2d;
        else
            Canvas.worldCamera = car;
    }
    public void DoResetOrder(int nOrder)
    {
        if (Canvas != null)
            SortOrder = nOrder;// m_nLayerZero + i;
        Canvas.planeDistance = 100; // 这里保证能看到的距离

        UISortingOrderBase[] so = GetComponentsInChildren<UISortingOrderBase>(true);
        for (int i = 0; i < so.Length; i++)
        {
            so[i].SetOrder(nOrder);
        }

        LineRenderer[] pathRenders = GetComponentsInChildren<LineRenderer>(true);
        foreach (var render in pathRenders)
            render.sortingOrder = nOrder + 1;// 如果同层，画线会被ui挡住

        UIMgr.Instance().SetCameraEnable(m_nCamera);
    }

    bool m_bCache;
    public bool IsCache
    {
        get { return m_bCache; }
        set { m_bCache = value; }
    }

    public virtual void DoRelease()
    {
        gameObject.SetActive(false);
        if (!IsCache)
        {
            GameObject.Destroy(gameObject);
        }

    }

    bool m_bUseTemp = false;
    public void DoPushTemp()
    {
        UIMgr.Instance().DoPushWindow(m_strPanelName, null);
        m_bUseTemp = true;
    }

    public void DoClearTemp()
    {
        m_bUseTemp = false;
        UIMgr.Instance().DoClearTmpWindow();
    }
    void PopTemp()
    {
        UIMgr.Instance().DoPopWindow(m_strPanelName);
    }

    public GameObject GetNodeByName(string strName)
    {
        return gameObject.FindDeepChildByName(strName);
    }


}

